package com.shadow.baiqun.delegate.receive;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.shadow.baiqun.config.ItemField;
import com.shadow.baiqun.config.ItemType;
import com.shadow.ui.recycler.DataConverter;
import com.shadow.ui.recycler.MultipleItemEntity;
import com.shadow.ui.recycler.ObjConverter;

import java.util.ArrayList;

/**
 * @author zy
 * on 2018/6/26
 */
public class SupplierConverter extends DataConverter{

    @Override
    public ArrayList<MultipleItemEntity> convert() {
        try {
            final JSONArray jsonArray = JSONObject.parseObject(getJsonData())
                    .getJSONArray("rows");
            final int size = jsonArray.size();
            for (int i = 0; i < size; i++) {
                final JSONObject object = jsonArray.getJSONObject(i);
                SupplierEntry supplierEntry = new ObjConverter<SupplierEntry>().
                        convert(object.toJSONString(), new TypeReference<SupplierEntry>() {
                        });
                MultipleItemEntity itemEntity = MultipleItemEntity.builder()
                        .setItemType(ItemType.ITEM_SUPPLIER)
                        .setField(ItemField.ITEM_OBJ,supplierEntry)
                        .build();
                mEntities.add(itemEntity);
            }
            return mEntities;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
